using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System.Linq;

namespace TripleMatch
{
    public class DecorationsGroup : MonoBehaviour
    {
        [SerializeField] Transform m_decorationsContainer;
        [SerializeField] GameObject m_editToolsContainer;
        [SerializeField] GameObject m_decorationsBlock;

        [SerializeField] DecorationElement m_decorationElementPrefab;

        [SerializeField] Button m_buttonConfirm;
        [SerializeField] Button m_buttonRevert;
        [SerializeField] Button m_buttonBox;

        [SerializeField] GameObject m_boxContainer;
        [SerializeField] GameObject m_boxCellContainer;
        [SerializeField] DecorationCard m_decorationCardPrefab;
        [SerializeField] Button m_buttonBoxClose;

        Action<bool> m_actionSwitchToEdit;

        private void Start()
        {
            EventTriggerListener.Get(m_decorationsBlock).onDown += OnBlockerDown;
            EventTriggerListener.Get(m_decorationsBlock).onUp += OnBlockUp;

            m_buttonConfirm.onClick.AddListener(OnClickButtonConfirm);
            m_buttonRevert.onClick.AddListener(OnClickButtonRevert);
            m_buttonBox.onClick.AddListener(OnClickButtonBox);
            m_buttonBoxClose.onClick.AddListener(OnClickBoxClose);

            m_tempData = new Dictionary<int, List<DecorationInUseData>>();
        }

        #region 初始化

        /// <summary>
        /// 初始化当前装扮
        /// </summary>
        public void InitDecorations(Action<bool> onSwitchToEdit)
        {
            m_actionSwitchToEdit = onSwitchToEdit;
            m_tempData = DecorationsManager.S.GetDecorationsInUse();
            //初始化装饰物
            foreach (var item in m_tempData)
            {
                for (int i = 0; i < item.Value.Count; i++)
                {
                    DecorationElement decorationElement = GameObject.Instantiate(m_decorationElementPrefab, m_decorationsContainer);

                    if (!m_decorationElements.ContainsKey(item.Value[i].id)) m_decorationElements.Add(item.Value[i].id, new List<DecorationElement>());
                    decorationElement.InitElementData(item.Value[i].id);
                    decorationElement.transform.position = item.Value[i].pos;
                    m_decorationElements[item.Value[i].id].Add(decorationElement);
                }
            }
        }

        #endregion


        #region 编辑模式下按钮功能

        private Dictionary<int, List<DecorationInUseData>> m_tempData;
        private Dictionary<int, List<DecorationElement>> m_decorationElements = new Dictionary<int, List<DecorationElement>>();

        private Dictionary<int, DecorationCard> m_decorationCard = new Dictionary<int, DecorationCard>();

        private void OnClickBoxClose()
        {
            m_boxContainer.SetActive(false);
        }

        private void OnClickButtonBox()
        {
            if (m_decorationCard.Count == 0)
            {
                List<DecorationData> decorationDatas = DecorationsManager.S.GetDecorationDatas();
                for (int i = 0; i < decorationDatas.Count; i++)
                {
                    int countInUse = !m_decorationElements.ContainsKey(decorationDatas[i].id) ? 0 : m_decorationElements[decorationDatas[i].id].Count;
                    int countCanUse = decorationDatas[i].ownCount;
                    if (countCanUse > countInUse)
                    {
                        DecorationInfoData decorationInfoData = DecorationsManager.S.GetDecorationInfoData(decorationDatas[i].id);
                        DecorationCard decorationCard = GameObject.Instantiate(m_decorationCardPrefab, m_boxCellContainer.transform);
                        decorationCard.InitDecorationData(decorationDatas[i].id, decorationInfoData.sprite, countCanUse - countInUse, OnClickUse);
                        m_decorationCard.Add(decorationDatas[i].id, decorationCard);
                    }
                }
            }

            m_boxContainer.SetActive(true);
        }

        private void OnClickUse(int id, DecorationCard decorationCard)
        { 
            DecorationElement decorationElement = GameObject.Instantiate(m_decorationElementPrefab, m_decorationsContainer);

            if (!m_decorationElements.ContainsKey(id)) m_decorationElements.Add(id, new List<DecorationElement>());
            decorationElement.InitElementData(id);
            decorationElement.transform.position = Vector2.zero;
            m_decorationElements[id].Add(decorationElement);

            int countInUse = m_decorationElements[id].Count;
            int countCanUse = DecorationsManager.S.GetDecorationData(id).ownCount;
            if (countCanUse > countInUse)
                decorationCard.RefreshData(countCanUse - countInUse);
            else
            {
                m_decorationCard.Remove(id);
                GameObject.Destroy(decorationCard.gameObject);
            }
        }

        private void OnClickButtonRevert()
        {
            RevertTempData();
            SwitchToDisplayMode();
        }

        private void OnClickButtonConfirm()
        {
            SaveTempData();
            SwitchToDisplayMode();
        }

        private void SaveTempData()
        {
            m_tempData.Clear();
            DecorationInUseData decorationInUseData = null;
            foreach (var item in m_decorationElements)
            {
                for (int i = 0; i < item.Value.Count; i++)
                {
                    decorationInUseData = new DecorationInUseData();
                    decorationInUseData.id = item.Value[i].displayDecorationId;
                    decorationInUseData.pos = item.Value[i].transform.position;
                    if (!m_tempData.ContainsKey(decorationInUseData.id))
                        m_tempData.Add(decorationInUseData.id, new List<DecorationInUseData>());

                    m_tempData[decorationInUseData.id].Add(decorationInUseData);
                    m_tempData[decorationInUseData.id].Last().index = m_tempData[decorationInUseData.id].Count;
                    m_tempData[decorationInUseData.id].Last().mixId = decorationInUseData.id * 10000 + m_tempData[decorationInUseData.id].Count;
                }
            }
        }

        private void RevertTempData()
        {
            foreach (var item in m_decorationElements)
            {
                for (int i = 0; i < item.Value.Count; i++)
                {
                    GameObject.Destroy(item.Value[i].gameObject);
                }
            }


            m_decorationElements.Clear();

            //初始化装饰物
            foreach (var item in m_tempData)
            {
                for (int i = 0; i < item.Value.Count; i++)
                {
                    DecorationElement decorationElement = GameObject.Instantiate(m_decorationElementPrefab, m_decorationsContainer);

                    if (!m_decorationElements.ContainsKey(item.Value[i].id)) m_decorationElements.Add(item.Value[i].id, new List<DecorationElement>());
                    decorationElement.InitElementData(item.Value[i].id);
                    decorationElement.transform.position = item.Value[i].pos;
                    m_decorationElements[item.Value[i].id].Add(decorationElement);
                }
            }
        }

        #endregion

        #region 编辑模式/展示模式的长按切换

        private bool m_isPress;
        private float m_blockPressTime;

        private void SwitchToEditMode()
        {
            m_actionSwitchToEdit?.Invoke(true);
            m_decorationsBlock.SetActive(false);
            m_editToolsContainer.SetActive(true);
        }

        private void SwitchToDisplayMode()
        {
            m_actionSwitchToEdit?.Invoke(false);
            m_decorationsBlock.SetActive(true);
            m_editToolsContainer.SetActive(false);
        }

        private void OnBlockUp(GameObject go)
        {
            if (m_isPress) m_isPress = false;
        }

        private void OnBlockerDown(GameObject go)
        {
            if (!m_isPress) m_isPress = true;
        }

        private void Update()
        {
            if (m_isPress)
            {
                m_blockPressTime += Time.deltaTime;
                if (m_blockPressTime >= 1f)
                {
                    SwitchToEditMode();
                    m_blockPressTime = 0f;
                    m_isPress = false;
                }
            } 
        }

        #endregion

    }
}

